import React from 'react';

import userService from '@/api/services/security/userService';
import DebounceSelect from '@/components/debounce-select';

export interface UserSelectProps {
  value?: {
    key: string;
    label: string;
    value: string;
    meta: {};
  };
  onChange?: (value: { key: string; label: string; value: string; meta: {} }) => void;
  defaultOption?: {};
}
export default function SelectUser({ value, onChange, defaultOption }: UserSelectProps) {
  const userInfo = async (qs: string) => {
    const res = await userService.getUserList({ take: 10, page: 1, name: qs });
    return res.data.map((item): any => ({
      label: item.name,
      value: item.id,
    }));
  };
  return (
    <DebounceSelect
      showSearch
      defaultOptions={[defaultOption]}
      fetchOptions={userInfo}
      value={value}
      placeholder="请输入用户昵称搜索"
      onChange={(_, option: any) => {
        onChange?.(option);
      }}
    />
  );
}
